<template>
	<view class="container">
		<view class="product-list flex-space-between flex-wrap">
			<view class="product-item bg-fff" v-for="(item,index) in productList" :key="index" @click="toDetail(item)">
				<image
                    v-if="item['mark']"
                    class="mark-at-top"
                    :src="item['mark']['icon']"
                    mode="widthFix"
                    :style="
                        item.mark.position == 0
                            ? 'top:' +
                              item.mark.t_margin +
                              'px; left:' +
                              item.mark.l_margin +
                              'px;'
                            : 'top:' +
                              item.mark.t_margin +
                              'px; right:' +
                              item.mark.l_margin +
                              'px;'
                    "
                ></image>
				<image class="product-item-image" :src="item.product_image"></image>
				<view class="product-con">
					<view class="product-con-name u-line-2">
						<span class="product-label" v-if="item['label']">{{item['label']['label_name']}}</span>
						{{item.product_name}}
					</view>
					<view class="flex-space-between">
						<view class="product-con-price">
							<text class="product-big-price">{{item.product_price}}</text>
							<text class="product-del">{{item.product_sku.line_price}}</text>
						</view>
						<b-u-button-theme width="52rpx" height="40rpx" size="24rpx" @click="toDetail(item)">购买</b-u-button-theme>
					</view>
				</view>
			</view>
			
			<view class="product-item" v-if="productList.length % 2 == 1"></view>
		</view>
		
		<view v-if="productList.length > 0">
			<b-u-loadmore :status="loadmoreStatus"></b-u-loadmore>
		</view>
		<view v-else-if="loaded" style="padding-top: 100rpx;">
			<u-empty text="暂无数据" mode="data"></u-empty>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageForm: {
					category_id: '',
					page: 1,
					list_rows: 20,
					show_price:1,
				},
				productList: [],
				loaded:false,
				loadmoreStatus: 'nomore',
			}
		},
		onLoad(options) {
			uni.setNavigationBarTitle({
				title: options.cname
			})
			this.pageForm.category_id = options.cid;
			this.getHttp();
		},
		onReachBottom() {
			if(this.loadmoreStatus == 'nomore') {
				return;
			}
			this.pageForm.page++;
			this.getProductLists();
		},
		methods: {
			toDetail(item){
				// console.log('item',item)
				if(item.categorydetail.type==1){
					this.jump('/pages/categoty/product/product_no_cart?id='+item.product_id)
				}else{
					this.jump('/pages/categoty/product/product?id='+item.product_id)
				}
			},
			jump(url) {
				uni.navigateTo({
					url: url
				})
			},
			getProductLists() {
				this.loadmoreStatus = 'loading';
				uni.$u.api.getProductLists(this.pageForm).then(res => {
					this.productList = this.productList.concat(res.list.data);
					this.loadmoreStatus = 'loadmore';
					if(this.productList.length  == res.list.total) {
						this.loadmoreStatus = 'nomore';
					}
					this.loaded = true;
				})
			},
			getHttp() {
				this.getProductLists();
			},
		},
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 30rpx;
		
		.product-list {
			
			.product-item {
				width: 334rpx;
				position: relative;
				border-radius: 16rpx;
				margin-bottom: 24rpx;
				overflow: hidden;
			}
			
			.product-item-image {
				width: 334rpx;
				height: 328rpx;
			}
			
			.product-con {
				padding: 12rpx 20rpx 18rpx;
				
				.product-con-name {
					font-size: 26rpx;
					color: #111111;
					font-weight: bold;
					margin-bottom: 14rpx;
					min-height: 68rpx;
				}
				
				.product-con-price {
					display: flex;
					flex-direction: column;
					.product-big-price{
						font-size: 36rpx;
						font-weight: bold;
						color: $uni-color-theme;
						line-height: 40rpx;
						&::before{
							font-size: 24rpx;
							content: "￥";
						}
					}
					.product-del{
						font-size: 22rpx;
						text-decoration: line-through;
						color: #CCCCCC;
						line-height: 30rpx;
						&::before{
							font-size: 20rpx;
							content: "￥";
						}
					}
				}
			}
		}
	}
	.mark-at-top {
		position: absolute;
		z-index: 80;
		width: 120rpx;
	}
	.product-label {
		background: #BB0024;
		padding: 0rpx 10rpx 2rpx 10rpx;
		color: #fff;
		font-size: 24rpx;
		border-radius: 10rpx;
		margin-right: 10rpx;
	}
</style>
